package com.arron.state;

public class Account {
    private AccountState state; //维持一个对抽象状态对象的引用
    private String owner;       //开户名
    private double balance = 0; //账户余额

    public Account(String owner, double balance) {
        this.state = new NormalState(this);
        this.owner = owner;
        this.balance = balance;
        System.out.println(this.owner + "开户，初始金额为" + balance);
    }

    public AccountState getState() {
        return state;
    }

    public void setState(AccountState state) {
        this.state = state;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void deposit(double amount) {
        System.out.println(this.owner + "存款" + amount);
        state.deposit(amount);//调用状态对象的deposit方法
        System.out.println("现在余额为" + this.balance);
        System.out.println("现在账户状态为" + this.state.getClass().getName());
        System.out.println("------------------------------------");
    }

    public void withdraw(double amount) {
        System.out.println(this.owner + "取款" + amount);
        state.withdraw(amount); //调用状态对象的withdraw方法
        System.out.println("现在余额为" + this.balance);
        System.out.println("现在账户状态为" + this.state.getClass().getName());
        System.out.println("-------------------------------------");
    }

    public void computerInterest() {
        state.computerInterest();//调用状态对象的computerInterest方法
    }
}